package com.duyi.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * @author ctx
 * @date 2020/2/16
 * @description
 */
@Controller
public class LoginController {

    //访问登录页面
    @GetMapping("/toLogin")
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        HttpSession session){
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            //登陆成功
            session.setAttribute("loginUser",username);
            return "redirect:/list";
        }else{
            //登陆失败重新登陆
            return "login";
        }
    }
}
